home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / joycheck.c < prev    next >
C/C++ Source or Header  |  1993-10-17  |  9KB  |  326 lines

  1. ;/* joycheck.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 joycheck.c
  3. Blink FROM LIB:c.o,joycheck.o TO joycheck LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7.  
  8. #include <exec/types.h>
  9. #include <intuition/intuition.h>
  10. #include <libraries/dos.h>
  11. #include "libraries/lowlevel.h"
  12.  
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/lowlevel_protos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "pragmas/lowlevel_pragmas.h"
  24.  
  25. #ifdef LATTICE
  26. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  27. void chkabort(void) { return; }  /* really */
  28. #endif
  29.  
  30. UBYTE version[] = "$VER: 39.3 joycheck";
  31.  
  32.  
  33.  
  34. /* Mask #define with result of ReadJoyPort(). */
  35. #define MSK(MASK) ( MASK & state )
  36.  
  37. struct Library *IntuitionBase = NULL;
  38. struct Library *GfxBase = NULL;
  39. struct Library *LowLevelBase = NULL;
  40.  
  41. struct Screen *scr = NULL;
  42. struct Window *win = NULL;
  43. struct RastPort *rp;
  44. struct BitMap *bitmap;
  45. UBYTE *plane;
  46. UWORD  bpr;
  47.  
  48. /* in bytes */
  49. #define PX0    (0)
  50. #define PX1    (0)
  51.  
  52. /* in scan lines / pixels */
  53. #define HELPX   (16)
  54. #define HELPY    (40)
  55. #define TEXTX   (PX0<<3)
  56. #define TEXTY    (64)
  57. #define PY0    (52)
  58. #define PY1    (116)
  59.  
  60. UWORD xpos[2] = { PX0, PX1 };
  61. UWORD ypos[2] = { PY0, PY1 };
  62.  
  63.  
  64. UBYTE *help = 
  65. "To Exit - click close gadget OR hold PORT1 A&B (BLUE&RED) down a long time";
  66.  
  67. UBYTE *labels[] = {
  68. "0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 PORT 0 ABOVE",
  69. "h h h h h h h h v v v v v v v v <- Mouse    CType->none 0 0 0 0",
  70. "J J J J                           P R F G Y R B    game 1 0 0 0",
  71. "O O O O                           L V O R E E L   mouse 0 1 0 0",
  72. "Y Y Y Y                           A S R E L D U   joyst 1 1 0 0",
  73. "R L D U                           Y   W E L   E     ??? 0 0 1 0",
  74. "0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 PORT 1 BELOW",
  75. "",
  76. "",
  77. "",
  78. "        _____          _____               _____          _____ ",
  79. "       |     |        |     |             |     |        |     |",
  80. "       |     |________|     |             |     |________|     |",
  81. "       |                    |             |                    |",
  82. "       |                    |             |                    |",
  83. "       |____________________|             |____________________|",
  84. /*
  85.          --X--          --X--
  86.          -----          -----
  87.          --X-------------X-X-
  88.          X---X-------X---X-X-
  89.          --X-----------------
  90. */
  91. NULL };
  92.  
  93. /* Button offsets -  X in bytes, Y in lines                                */
  94. #define BXSTART0  (8)
  95. #define BXSTART1  (43)
  96. #define BYSTART   (TEXTY+(11*8)-6)
  97.  
  98.  
  99. /*                 R    L    D    U    P    V    F    G    Y    R    B     */
  100. UWORD bxoffs[] = { 4,   0,   2,   2,  12,   2,  17,  16,  18,  16,  18 }; 
  101. UWORD byoffs[] = { 3*8, 3*8, 4*8, 2*8, 3*8, 0*8, 0*8, 2*8, 2*8, 3*8, 3*8 };
  102.  
  103. BOOL cleared[2] = { TRUE, TRUE };
  104.  
  105. struct TextAttr topaz80 =
  106.    {"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT };
  107.  
  108. VOID bye(UBYTE *msg, LONG err )
  109. {
  110.     if(*msg)        printf("%s\n",msg);
  111.     if(win)        CloseWindow(win);
  112.     if(scr)        CloseScreen(scr);
  113.     if(GfxBase)        CloseLibrary(GfxBase);
  114.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  115.     if(LowLevelBase)    CloseLibrary(LowLevelBase);
  116.     exit(err);
  117. }
  118.  
  119. VOID showbits(ULONG state, ULONG port)
  120.     {
  121.     ULONG k, j, bset;
  122.     UWORD x, y, sbx, sby, bx, by;
  123.     UBYTE *spot;
  124.     int bi;
  125.  
  126.     x = xpos[port];    /* in bytes  */
  127.     y = ypos[port];    /* in pixels */
  128.  
  129.     sbx = port ? BXSTART0 : BXSTART1;
  130.     sby = BYSTART;
  131.  
  132.     for(k=0; k<32; k++, x+=2)
  133.     {
  134.     bset = state & (1L << k);
  135.         spot = plane + (y * bpr) + x;
  136.     if(bset)    for(j=0; j<3; j++, spot+=bpr)    *spot = 0xFF;
  137.     else        for(j=0; j<3; j++, spot+=bpr)   *spot = 0x00;
  138.  
  139.     if((state & JP_TYPE_MASK) == JP_TYPE_GAMECTLR )
  140.         {
  141.         cleared[port]=FALSE;
  142.         if( ((k>=0)&&(k<=3)) || ((k>=17)&&(k<=23)) )
  143.             {
  144.         bi = (k<=3) ? k : k - 13;
  145.         by = sby + byoffs[bi];        /* in lines */
  146.         bx = sbx + bxoffs[bi];        /* in bytes */
  147.             spot = plane + (by * bpr) + bx;
  148.         if(bset)    for(j=0; j<3; j++, spot+=bpr)    *spot = 0xFF;
  149.         else        for(j=0; j<3; j++, spot+=bpr)   *spot = 0x00;
  150.             }
  151.         }
  152.     else if((state & JP_TYPE_MASK) == JP_TYPE_JOYSTK )
  153.         {
  154.         cleared[port]=FALSE;
  155.         if( ((k>=0)&&(k<=3)) || ((k>=22)&&(k<=23)) )
  156.         {
  157.         bi = (k<=3) ? k : k - 13;
  158.         by = sby + byoffs[bi];        /* in lines */
  159.         bx = sbx + bxoffs[bi];        /* in bytes */
  160.             spot = plane + (by * bpr) + bx;
  161.         if(bset)    for(j=0; j<3; j++, spot+=bpr)    *spot = 0xFF;
  162.         else        for(j=0; j<3; j++, spot+=bpr)   *spot = 0x00;
  163.         }
  164.         }
  165.     else if(!(cleared[port]))
  166.         {
  167.         cleared[port]=TRUE;
  168.         if( ((k>=0)&&(k<=3)) || ((k>=17)&&(k<=23)) )
  169.         {
  170.         bi = (k<=3) ? k : k - 13;
  171.         by = sby + byoffs[bi];        /* in lines */
  172.         bx = sbx + bxoffs[bi];        /* in bytes */
  173.             spot = plane + (by * bpr) + bx;
  174.         for(j=0; j<3; j++, spot+=bpr)   *spot = 0x00;
  175.         }
  176.         }
  177.     }
  178.     }
  179.  
  180. void main( int argc, char **argv)
  181. {
  182. ULONG port = 0L;
  183. ULONG state = 0L, oldstate = (ULONG) JP_TYPE_NOTAVAIL;
  184. ULONG state0, state1, heldtwo, htcnt;
  185. BYTE x1,y1,x2,y2,diffx,diffy;
  186. int k;
  187.  
  188.     if(!argc)    exit(RETURN_FAIL);
  189.  
  190.     IntuitionBase     = OpenLibrary("intuition.library",39);
  191.     GfxBase         = OpenLibrary("graphics.library",39);
  192.     LowLevelBase     = OpenLibrary("lowlevel.library",39);
  193.  
  194.     if((!IntuitionBase)||(!GfxBase)||(!LowLevelBase))
  195.     {
  196.     bye("Error opening Intuition, Graphics, or LowLevel library V39+",
  197.         RETURN_FAIL);
  198.     }
  199.  
  200.  
  201.     if(!(scr = OpenScreenTags(NULL,
  202.                 SA_DisplayID, HIRES,
  203.                 SA_Width,    640,
  204.                 SA_Height,    200,
  205.                 SA_Depth,      1,
  206.                 SA_Font,    &topaz80,
  207.                 SA_Title, "     JoyCheck",
  208.                 TAG_DONE)))
  209.     {
  210.     bye("Can't open screen",RETURN_FAIL);
  211.     }
  212.  
  213.     if(!(win = OpenWindowTags(NULL,
  214.                 WA_Width,     40,
  215.                 WA_Height,     20,
  216.                 WA_Flags,     WFLG_CLOSEGADGET,
  217.                 WA_IDCMP,     IDCMP_CLOSEWINDOW,
  218.                 WA_CustomScreen, scr,
  219.                 TAG_DONE)))
  220.     {
  221.     bye("Can't open window",RETURN_FAIL);
  222.     }
  223.  
  224.     rp        = &scr->RastPort;
  225.     bitmap = scr->RastPort.BitMap;
  226.     bpr       = bitmap->BytesPerRow;
  227.     plane  = bitmap->Planes[0];
  228.  
  229.     SetAPen(rp,1);
  230.  
  231.     Move(rp,HELPX,HELPY);
  232.     Text(rp,help,strlen(help));
  233.  
  234.     for(k=0; labels[k]; k++)
  235.     {
  236.         Move(rp,TEXTX,TEXTY + (k<<3));
  237.     Text(rp,labels[k],strlen(labels[k]));
  238.     }
  239.  
  240.     htcnt=0;
  241.  
  242.     /* While the CLOSE gadget hasn't been hit... */
  243.     while ( 0L == CheckSignal( 1L<<win->UserPort->mp_SigBit ) )
  244.     {
  245.     state0 = ReadJoyPort(0);
  246.     state1 = ReadJoyPort(1);
  247.     showbits(state0,0);
  248.     showbits(state1,1);
  249.  
  250.     /* If Left and Right buttons held long, exit */
  251.     if((state1&(JPF_BUTTON_BLUE|JPF_BUTTON_RED))==
  252.             (JPF_BUTTON_BLUE|JPF_BUTTON_RED))    htcnt++;
  253.     else htcnt=0;
  254.     if(htcnt > 3000)    break;
  255.     
  256. #ifdef OLDSTUFF
  257.     oldstate = state;
  258.     printf("\nPort %1ld:  ", port);
  259.     state = ReadJoyPort( port );
  260.  
  261.     if (oldstate == state)
  262.             puts("No change.");
  263.     else
  264.         {
  265.         switch ( MSK(JP_TYPE_MASK) )
  266.         {
  267.         case (JP_TYPE_NOTAVAIL):
  268.             puts("Port data unavailable.");
  269.         break;
  270.  
  271.         case (JP_TYPE_GAMECTLR):
  272.             puts("Game controller:");
  273.             if (MSK(JPF_BUTTON_BLUE))    puts("    Stop/BLUE");
  274.             if (MSK(JPF_BUTTON_RED))     puts("    Select/RED");
  275.             if (MSK(JPF_BUTTON_YELLOW))    puts("    Repeat/YELLOW");
  276.             if (MSK(JPF_BUTTON_GREEN))    puts("    Shuffle/GREEN");
  277.             if (MSK(JPF_BUTTON_FORWARD))    puts("    Right ear");
  278.             if (MSK(JPF_BUTTON_REVERSE))    puts("    Left ear");
  279.             if (MSK(JPF_BUTTON_PLAY))    puts("    Pause/Middle");
  280.             if (MSK(JPF_JOY_UP))        puts("    Up direction");
  281.             if (MSK(JPF_JOY_DOWN))        puts("    Down direction");
  282.             if (MSK(JPF_JOY_LEFT))        puts("    Left direction");
  283.             if (MSK(JPF_JOY_RIGHT))        puts("    Right direction");
  284.         break;
  285.  
  286.         case (JP_TYPE_JOYSTK):
  287.             puts("Joystick:");
  288.             if (MSK(JPF_BUTTON_BLUE))    puts("    Right");
  289.             if (MSK(JPF_BUTTON_RED))    puts("    Fire");
  290.             if (MSK(JPF_JOY_UP))        puts("    Up direction");
  291.             if (MSK(JPF_JOY_DOWN))        puts("    Down direction");
  292.             if (MSK(JPF_JOY_LEFT))        puts("    Left direction");
  293.             if (MSK(JPF_JOY_RIGHT))        puts("    Right direction");
  294.         break;
  295.  
  296.         case (JP_TYPE_MOUSE):
  297.             puts("Mouse:");
  298.  
  299.             x1 = MSK(JP_MHORZ_MASK);
  300.             y1 = (MSK(JP_MVERT_MASK) >> 8);
  301.             state = ReadJoyPort( port );
  302.             x2 = MSK(JP_MHORZ_MASK);
  303.             y2 = (MSK(JP_MVERT_MASK) >> 8);
  304.             diffx = x2-x1;
  305.             diffy = y2-y1;
  306.             if (MSK(JPF_BUTTON_BLUE))    puts("    Right mouse button");
  307.             if (MSK(JPF_BUTTON_RED))    puts("    Left mouse button");
  308.             if (MSK(JPF_BUTTON_PLAY))    puts("    Middle mouse button");
  309.             printf("\tx: %d\ty: %d\n",diffx,diffy); 
  310.         break;
  311.  
  312.         case (JP_TYPE_UNKNOWN):
  313.             puts("Unknown device.");
  314.         break;
  315.  
  316.         default:                    /* default */
  317.             puts("WARNING: Unexpected JP_TYPE!");
  318.         break;
  319.         }
  320.         }
  321. #endif
  322.  
  323.     }
  324.     bye("",RETURN_OK );
  325. }
  326.